﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EntityComponentSystem.Framework;
using EntityComponentSystem.Framework.Systems;

namespace ECS.Demos.Tetris.Systems
{
    public interface IInputSystem : IBehaviorSystem
    {
        bool IsKeyDown(params ConsoleKey[] key);
        bool AnyKeyDown(params ConsoleKey[] key);

    }

    public class InputSystem : BehaviorSystem, IInputSystem
    {
        private HashSet<ConsoleKey> keysDown = new HashSet<ConsoleKey>();

        public InputSystem()
        {
            this.Name = "input";
        }

        public override void Update(GameEngine ge)
        {
            keysDown.Clear();
            if (Console.KeyAvailable)
            {
                while (Console.KeyAvailable)
                {
                    var key = Console.ReadKey();
                    keysDown.Add(key.Key);

                    //if(key.Key == ConsoleKey.Escape)
                        
                    }
            }

            base.Update(ge);
        }

        public bool IsKeyDown(params ConsoleKey[] key)
        {
            for (var i = 0; i < key.Length; i++)
                if (!keysDown.Contains(key[i]))
                    return false;

            return true;
        }

        public bool AnyKeyDown(params ConsoleKey[] key)
        {
            for (var i = 0; i < key.Length; i++)
                if (keysDown.Contains(key[i]))
                    return true;

            return false;
        }
    }
}
